/**
 * Copyright (c) 2016-2021 by the respective copyright holders.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 */
package com.zsmartsystems.zigbee.dongle.telegesis.internal.protocol;

import com.zsmartsystems.zigbee.ExtendedPanId;
import java.util.ArrayList;
import java.util.List;

/**
 * Class to implement the Telegesis command <b>Active Scan</b>.
 * <p>
 * Scan For Active PANs
 * <p>
 * This class provides methods for processing Telegesis AT API commands.
 * <p>
 * Note that this code is autogenerated. Manual changes may be overwritten.
 *
 * @author Chris Jackson - Initial contribution of Java code generator
 */
public class TelegesisActiveScanCommand extends TelegesisFrame implements TelegesisCommand {
    /**
     * Command field
     */
    private Integer channelMask;

    /**
     * +PANSCAN response field
     */
    private List<ScanResult> scanResults = new ArrayList<ScanResult>();

    /**
     *
     * @param channelMask the channelMask to set as {@link Integer}
     */
    public void setChannelMask(Integer channelMask) {
        this.channelMask = channelMask;
    }

    /**
     *
     * @return the scanResults as a {@link List} of {@link ScanResult}
     */
    public List<ScanResult> getScanResults() {
        return scanResults;
    }

    @Override
    public int[] serialize() {
        // Serialize the command fields
        serializeCommand("AT+PANSCAN");
        serializeDelimiter();
        serializeInt16(channelMask);

        return getPayload();
    }

    @Override
    public boolean deserialize(int[] data) {
        // Handle standard status responses (ie. OK / ERROR)
        if (handleIncomingStatus(data)) {
            return true;
        }

        initialiseDeserializer(data);

        // Deserialize the fields for the "+PANSCAN" response
        if (testPrompt(data, "+PANSCAN:")) {
            scanResults.add(new ScanResult(data));
            return false;
        }

        return false;
    }

    @Override
    public String toString() {
        final StringBuilder builder = new StringBuilder(836);
        // First present the command parameters...
        // Then the responses later if they are available
        builder.append("TelegesisActiveScanCommand [channelMask=");
        builder.append(channelMask);
        builder.append(", scanResults=");
        builder.append(scanResults);
        if (status != null) {
            builder.append(", status=");
            builder.append(status);
        }
        builder.append(']');
        return builder.toString();
    }

    /**
     *
     */
    public class ScanResult extends TelegesisFrame {
        /**
         */
        private Integer channel;

        /**
         */
        private Integer panId;

        /**
         */
        private ExtendedPanId epanId;

        /**
         */
        private Integer profileId;

        /**
         */
        private Boolean joiningEnabled;

        /**
         */
        private Integer rssi;

        /**
         */
        private Integer lqi;

        /**
         * Constructor to deserialize the received data
         */
        ScanResult(final int[] data) {
            initialiseDeserializer(data);
            setDeserializer(9);

            // Deserialize field "channel"
            channel = deserializeDec8();
            stepDeserializer();

            // Deserialize field "pan id"
            panId = deserializeInt16();
            stepDeserializer();

            // Deserialize field "EPan id"
            epanId = deserializeExtendedPanId();
            stepDeserializer();

            // Deserialize field "profile id"
            profileId = deserializeInt8();
            stepDeserializer();

            // Deserialize field "joining enabled"
            joiningEnabled = deserializeBoolean();
            stepDeserializer();

            // Deserialize field "rssi" [optional]
            pushDeserializer();
            rssi = deserializeSInt8();
            if (rssi == null) {
                popDeserializer();
            } else {
                stepDeserializer();
            }

            // Deserialize field "lqi" [optional]
            lqi = deserializeInt8();
        }

        /**
         *
         * @return the channel as {@link Integer}
         */
        public Integer getChannel() {
            return channel;
        }

        /**
         *
         * @return the panId as {@link Integer}
         */
        public Integer getPanId() {
            return panId;
        }

        /**
         *
         * @return the epanId as {@link ExtendedPanId}
         */
        public ExtendedPanId getEpanId() {
            return epanId;
        }

        /**
         *
         * @return the profileId as {@link Integer}
         */
        public Integer getProfileId() {
            return profileId;
        }

        /**
         *
         * @return the joiningEnabled as {@link Boolean}
         */
        public Boolean getJoiningEnabled() {
            return joiningEnabled;
        }

        /**
         *
         * @return the rssi as {@link Integer}
         */
        public Integer getRssi() {
            return rssi;
        }

        /**
         *
         * @return the lqi as {@link Integer}
         */
        public Integer getLqi() {
            return lqi;
        }

        @Override
        public String toString() {
            final StringBuilder builder = new StringBuilder(254);
            builder.append("ScanResult [channel=");
            builder.append(channel);
            builder.append(", panId=");
            builder.append(panId);
            builder.append(", epanId=");
            builder.append(epanId);
            builder.append(", profileId=");
            builder.append(profileId);
            builder.append(", joiningEnabled=");
            builder.append(joiningEnabled);
            builder.append(", rssi=");
            builder.append(rssi);
            builder.append(", lqi=");
            builder.append(lqi);
            builder.append(']');
            return builder.toString();
        }
    }
}
